package com.webinsky.common.utils;

import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;

public class JsonUtils {
	/**
	 * 解析对象成string
	 * 
	 * @param data
	 * @return
	 */
	public static <T> String toStr(T data){
		if ( data != null ){
			Gson gson = new Gson();
			return gson.toJson(data);
		}
		return null;
	}
	
	public static <T> T fromStr(String json, Class<T> clazz){
		if ( json != null && clazz != null ){
			Gson gson = new Gson();
			return gson.fromJson(json, clazz);
		}
		return null;
	}
	
	public static class Tester {
		private int id;
		private String name;
		
		public Tester(int id, String name){
			this.id = id;
			this.name = name;
		}
		
		public int getId() {
			return id;
		}
		public void setId(int id) {
			this.id = id;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
	}
	
	public static void main(String args[]){
		List<Tester> tests = new ArrayList<>();
		tests.add(new Tester(1, "Joe"));
		tests.add(new Tester(2, "Mary"));
		System.out.println(toStr(tests));
		
	}
}
